package com.szm.lnr.globle;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;

import com.szm.lnr.MainActivity;

import java.util.ArrayList;

/**
 * Created by szm on 2018/6/25.
 */

public class LarApplication extends Application {

    private static ArrayList<Activity> list = new ArrayList<>();
    private static LarApplication application;

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        /** 异常处理器初始化 */
        ReStartExceptionHandler.getInstance().initReStartExceptionHandler(this);
    }

    /**
     * Activity关闭时，删除Activity列表中的Activity对象*/
    public static void removeActivity(Activity a){
        list.remove(a);
    }

    /**
     * 向Activity列表中添加Activity对象*/
    public static void addActivity(Activity a){
        list.add(a);
    }

    /**
     * 关闭Activity列表中的所有Activity*/
    public static void finishActivity(){
        for (Activity activity : list) {
            if (null != activity) {
                activity.finish();
            }
        }
        //杀死该应用进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 关闭Activity列表中的所有Activity，并启动HomeActivity*/
    public static void restartApp(Context context){
        for (Activity activity : list) {
            if (null != activity && !activity.getClass().equals(MainActivity.class)) {
                activity.finish();
            }
        }
        //重新启动主activity
        context.startActivity(new Intent(context, MainActivity.class));
    }

    public static LarApplication getApp() {
        return application;
    }
}
